// package-activity/pages/activity-list/index.js
import { ManageTargetInfoUpdateMixins, ActivityMixins, ActivityListMixins } from '../../../behaviors/page';
import { ACTIVITY_OWNER_VALUE, ACTIVITY_LIST_TABS, ACTIVITY_MENU_ROUTE } from '../../../constants/activity';
import { navTo, showLoading, hideLoading } from '../../../common/env';
import { getActivityList } from '../../../api/brd';

Page({
    mixins: [ManageTargetInfoUpdateMixins, ActivityMixins, ActivityListMixins],
    data: {
        activityList: [],
        keyword: '',
        tabs: ACTIVITY_LIST_TABS,
        activityCreateSelectPopup: {
            show: false,
        },
    },
    onShow() {
        this.setData({
            pageNo: 1,
            pageTime: new Date().getTime(),
        }, async () => {
            await this.getActivityListData(true);
        });
    },
    getActivityListData: async function (isReplace=false) {
        showLoading('加载中...');
        const { pageNo, pageSize, pageTime, activeIndex, activityList, manageTargetInfo, keyword } = this.data;
        let param = {
            pageNo,
            pageSize,
            ts: pageTime,
            keyword,
        };
        const isAll = activeIndex === 'all';
        if (isAll) {
            param = {
                ...param,
                isAll,
            };
        } else {
            param = {
                ...param,
                processStatus: ACTIVITY_LIST_TABS.filter(item => item.name === activeIndex)[0].value,
            };
        }
        const ownerInfo = {
            owner: ACTIVITY_OWNER_VALUE.brand,
            ownerName: manageTargetInfo.name,
            ownerPic: manageTargetInfo.pic,
            canManage: true,
        };
        const list = await getActivityList(param, ownerInfo);
        console.log('ManageActivityList => 获取的列表数据为：', list);
        this.setData({
            activityList: isReplace ? list : [...activityList, ...list],
        });
        hideLoading();
    },
    handleSearchChange(e) {
        this.setData({
            keyword: e.detail,
        });
    },
    handleSearch() {
        this.setData({
            pageNo: 1,
            pageTime: new Date().getTime(),
        }, async () => {
            await this.getActivityListData(true);
        });
    },
    handleShowActivityCreateSelect() {
        this.setData({
            'activityCreateSelectPopup.show': true,
        });
    },
    handleActivityCreateSelect(e) {
        const type = e.detail;
        const { manageTargetType } = this.data;
        let pageUrl = ACTIVITY_MENU_ROUTE[manageTargetType][type];
        navTo(pageUrl);
    },
})
